#include "mytoplabel.h"
#include <QDateTime>
#include <QTimer>
#include <QDebug>

MyTopLabel::MyTopLabel(QWidget *parent) : QLabel(parent)
{
    this->setPixmap(QPixmap("://imgs/main_ui/top.png"));
    this->setScaledContents(true);

    // 创建并配置时间标签
    m_timeLabel = new QLabel(this);
    m_timeLabel->setStyleSheet("QLabel {"
                            "color: white;"
                            "font-size: 24px;"
                            "font-weight: bold;"
                            "background-color: transparent;"
                            "padding: 10px;"
                            "border-radius: 5px;"
                            "}");
    m_timeLabel->setFixedHeight(60);
    updateTime(); // 初始化时间显示

    // 创建定时器，每秒更新一次时间
    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &MyTopLabel::updateTime);
    timer->start(1000);

    // 创建并配置名称标签
    m_nameLabel = new QLabel("视频存储软件", this);
    m_nameLabel->setStyleSheet("QLabel {"
                            "color: white;"
                            "font-size: 48px;"
                            "font-weight: bold;"
                            "background-color: transparent;"
                            "padding: 15px 30px;"
                            "border-radius: 10px;"
                            "}");
    m_nameLabel->setFixedHeight(80);

    // 创建并配置退出按钮
    m_exitButton = new QPushButton("退出系统", this);
    m_exitButton->setFixedSize(150, 60);
    m_exitButton->setStyleSheet("QPushButton {"
                              "color: white;"
                              "font-size: 24px;"
                              "font-weight: bold;"
                              "background-color: rgba(30, 60, 160, 200);"  // 深蓝色背景
                              "padding: 10px 20px;"
                              "border-radius: 5px;"
                              "border: 2px solid #a8c8ff;"  // 浅蓝色边框
                              "}"
                              "QPushButton:hover {"
                              "background-color: rgba(50, 80, 180, 220);"  // 悬停时颜色变亮
                              "}"
                              "QPushButton:pressed {"
                              "background-color: rgba(10, 40, 140, 220);"  // 按下时颜色变暗
                              "}");
    connect(m_exitButton, &QPushButton::clicked, this, &MyTopLabel::sigToClose);

    m_minButton = new QPushButton("最小化", this);
    m_minButton->setFixedSize(150, 60);
    m_minButton->setStyleSheet("QPushButton {"
                              "color: white;"
                              "font-size: 24px;"
                              "font-weight: bold;"
                              "background-color: rgba(30, 60, 160, 200);"  // 深蓝色背景
                              "padding: 10px 20px;"
                              "border-radius: 5px;"
                              "border: 2px solid #a8c8ff;"  // 浅蓝色边框
                              "}"
                              "QPushButton:hover {"
                              "background-color: rgba(50, 80, 180, 220);"  // 悬停时颜色变亮
                              "}"
                              "QPushButton:pressed {"
                              "background-color: rgba(10, 40, 140, 220);"  // 按下时颜色变暗
                              "}");
    connect(m_minButton, &QPushButton::clicked, this, &MyTopLabel::sigShowMin);

}

MyTopLabel::~MyTopLabel()
{
    //qDebug() << "这是一个测试，看看有没有进~MyTopLabel";
}

void MyTopLabel::resizeEvent(QResizeEvent *event)
{
    QLabel::resizeEvent(event);

    // 获取当前控件的宽度和高度
    int width = this->width();
    int height = this->height();

    // 计算各控件的位置
    // 时间标签：左对齐，垂直居中
    m_timeLabel->adjustSize();

    m_timeLabel->move(20, (height - m_timeLabel->height()) / 2);

    // 名称标签：水平居中，垂直居中
    m_nameLabel->adjustSize();
    int nameLabelWidth = m_nameLabel->width();
    m_nameLabel->move((width - nameLabelWidth) / 2, (height - m_nameLabel->height()) / 2);

    // 退出按钮：右对齐，垂直居中
    m_minButton->move(width - m_minButton->width() - m_exitButton->width() - 40, (height - m_minButton->height()) / 2);
    m_exitButton->move(width - m_exitButton->width() - 20, (height - m_exitButton->height()) / 2);
}

void MyTopLabel::updateTime()
{
    // 更新时间显示
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString timeString = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
    m_timeLabel->setText(timeString);
}
